package immutablecollections.exceptions;

/**
 * Thrown when you attempt to access an indexed `ImCollection` using an index that is not valid for that collection.
 */
@SuppressWarnings("serial")
public class ImIndexOutOfBoundsException extends RuntimeException
{
    public ImIndexOutOfBoundsException(String message)
    {
        super(message);
    }

    public static void check(int index, int size, String argumentName)
    {
        if (index < 1)
        {
            throw new ImIndexOutOfBoundsException(argumentName + " should be >= 1  but was " + index);
        }

        if (size == 0)
        {
            throw new ImIndexOutOfBoundsException("The collection is empty but " + argumentName + " was " + index);
        }

        if (index > size)
        {
            throw new ImIndexOutOfBoundsException("the size of the collection is " + size + " but " + argumentName
                    + " was " + index);
        }
    }

    public static void check0(int index, int size, String argumentName)
    {
        if (index < 0)
        {
            throw new ImIndexOutOfBoundsException(argumentName + " should be >= 0  but was " + index);
        }

        if (size == 0)
        {
            throw new ImIndexOutOfBoundsException("The collection is empty but " + argumentName + " was " + index);
        }

        if (index >= size)
        {
            throw new ImIndexOutOfBoundsException("the size of the collection is " + size + " but " + argumentName
                    + " was " + index);
        }
    }

    public static void check(int index, int min, int max, String argumentName)
    {
        if ((index < min) || (index > max))
        {
            throw new ImIndexOutOfBoundsException(argumentName + " should be in the range [" + min + ", " + max
                    + "]  but was " + index);
        }
    }
}
